package net.towee.client.com.lobby.view;

import net.towee.client.com.lobby.presenter.LobbyPeoplePresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.ExtFlowPanel;
import net.towee.client.widgets.SheduledInputBox;
import net.towee.model.account.AccountIdentity;
import net.towee.model.account.ContactsGroup;
import net.towee.model.utils.Pair;

import com.allen_sauer.gwt.dnd.client.DragEndEvent;
import com.allen_sauer.gwt.dnd.client.DragHandler;
import com.allen_sauer.gwt.dnd.client.DragStartEvent;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.allen_sauer.gwt.dnd.client.drop.FlowPanelDropController;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.inject.Inject;

public class LobbyPeopleView extends AbsolutePanel implements Display, HasOpenHandlers<String>,
		DragHandler {

	private final ClientResource resource;
	private final ToweeConstants constants;

	private final FlowPanel leftColumn;
	private final FlowPanel rightColumn;
	private SheduledInputBox searchInput;
	private FlowPanel foundedPeopleContainer;

	private PickupDragController foundedUserDragController;
	private NewGroup newGroup;

	private class FoundedUser extends ExtFlowPanel implements
			HasOpenHandlers<Pair<ContactsGroup, AccountIdentity>> {

		public FoundedUser(AccountIdentity account) {
			setStyleName(resource.css().lobbyPeopleFoundedUser());
			FlowPanel avatar = new FlowPanel();
			avatar.setStyleName(resource.css().lobbyPeopleAvatar());
			add(avatar);

			FlowPanel nickNameContainer = new FlowPanel();
			nickNameContainer.setStyleName(resource.css().lobbyPeopleNickNameContainer());
			add(nickNameContainer);

			Label name = new Label(account.getFullName());
			Label nick = new Label(account.getNickName());
			name.setStyleName(resource.css().lobbyPeopleName());
			nick.setStyleName(resource.css().lobbyPeopleNick());
			nickNameContainer.add(name);
			nickNameContainer.add(nick);
		}

		@Override
		public HandlerRegistration addOpenHandler(
				OpenHandler<Pair<ContactsGroup, AccountIdentity>> handler) {
			return addHandler(handler, OpenEvent.getType());
		}

	}

	private class NewGroup extends FlowPanel {

		public NewGroup() {
			setStyleName(resource.css().lobbyPeopleGroupContainer());
			getElement().getStyle().setProperty("border", "2px dashed #999");

			Label prompt = new Label(constants.lobbyPeopleAddNewGroup());
			prompt.setStyleName(resource.css().lobbyPeopleNewGroupPrompt());
			add(prompt);
		}

	}

	private class Group extends FlowPanel {

		public Group() {
			setStyleName(resource.css().lobbyPeopleGroupContainer());
		}

	}

	@Inject
	public LobbyPeopleView(ToweeConstants constants, ClientResource resources) {
		this.constants = constants;
		this.resource = resources;

		// Set height 100%
		getElement().getStyle().setHeight(100, Unit.PCT);

		// Left column (searching):
		leftColumn = new FlowPanel();
		leftColumn.setStyleName(resources.css().lobbyPeopleLeft());
		add(leftColumn);

		searchInput = new SheduledInputBox(constants.lobbyEnterWordkeys());
		FlowPanel inputContainer = new FlowPanel();
		inputContainer.setStyleName(resource.css().lobbyPeopleInputBox());
		inputContainer.add(searchInput);
		leftColumn.add(inputContainer);

		foundedPeopleContainer = new FlowPanel();
		foundedPeopleContainer.setStyleName(resource.css().lobbyFoundedPeopleContainer());
		leftColumn.add(foundedPeopleContainer);

		// initialize founded user drag controller
		foundedUserDragController = new PickupDragController(this, false);
		foundedUserDragController.setBehaviorMultipleSelection(false);

		// Right column (adding groups):
		rightColumn = new FlowPanel();
		rightColumn.setStyleName(resources.css().lobbyPeopleRight());
		add(rightColumn);

		newGroup = new NewGroup();
		rightColumn.add(newGroup);

		// initialize founded user drop controller
		FlowPanelDropController foundedUserDropController = new FlowPanelDropController(newGroup);
		foundedUserDragController.registerDropController(foundedUserDropController);

		foundedUserDragController.addDragHandler(this);

		addNewGroup();
		addNewGroup();
		addNewGroup();
		addNewGroup();
		addNewGroup();
		addNewGroup();
	}

	@Override
	public HasChangeHandlers getSearchBox() {
		return searchInput;
	}

	public void addNewGroup() {
		Group group = new Group();
		rightColumn.add(group);

		/*
		 * SheduledInputBox inputGroupName = new
		 * SheduledInputBox(constants.lobbyPeopleEnterGroupName()); FlowPanel
		 * inputGroupNameContainer = new FlowPanel();
		 * inputGroupNameContainer.setStyleName
		 * (resource.css().lobbyPeopleGroupNameContainer());
		 * inputGroupNameContainer.add(inputGroupName);
		 * group.add(inputGroupNameContainer);
		 */
	}

	@Override
	public String getSearchString() {
		return searchInput.getText();
	}

	@Override
	public HasOpenHandlers<Pair<ContactsGroup, AccountIdentity>> addFoundUser(AccountIdentity account) {
		FoundedUser foundedUser = new FoundedUser(account);
		foundedPeopleContainer.add(foundedUser);

		foundedUserDragController.makeDraggable(foundedUser);

		return foundedUser;
	}

	@Override
	public HasClickHandlers addAccountToContactGroup(AccountIdentity idnt, ContactsGroup group) {
		return new Button();
	}

	@Override
	public HasClickHandlers addContactsGroup(ContactsGroup group) {
		return new Button();
	}

	@Override
	public HasOpenHandlers<String> getAddContactGroupHandlers() {
		return this;
	}

	@Override
	public void removeContactsGroup(ContactsGroup g) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeAccountFromGroup(AccountIdentity second, ContactsGroup first) {
		// TODO Auto-generated method stub

	}

	@Override
	public HasClickHandlers getMoreResultButton() {
		// TODO Auto-generated method stub
		return new Button();
	}

	@Override
	public void clearOldSearchResult() {
		foundedPeopleContainer.clear();
	}

	@Override
	public HandlerRegistration addOpenHandler(OpenHandler<String> handler) {
		return addHandler(handler, OpenEvent.getType());
	}

	@Override
	public void onDragEnd(DragEndEvent event) {
	}

	@Override
	public void onDragStart(DragStartEvent event) {
	}

	@Override
	public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {
		// Check if user stop dragging on our drop target
		if (event.getContext().finalDropController != null) {

			if (newGroup.getElement().getAttribute("active").equals("")) {
				newGroup.getElement().setAttribute("active", "true");
				newGroup.clear();
			}

			FlowPanel avatar = new FlowPanel();
			avatar.setStyleName(resource.css().lobbyPeopleAvatarInGroup());
			newGroup.add(avatar);

			throw new VetoDragException();
		}
	}

	@Override
	public void onPreviewDragStart(DragStartEvent event) throws VetoDragException {
	}
}

// Anchor
